package com.logistics.pick.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.pick.entity.domain.PickContainer;
import com.logistics.pick.entity.model.PickContainerSave;
import com.logistics.pick.mapper.PickContainerMapper;
import com.logistics.utils.aspect.annotation.Insert;
import com.logistics.utils.constants.Message;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.message.Manager;
import com.logistics.utils.tool.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 * 拣配托盘 服务实现类
 * </p>
 *
 * @author yunjian.ban
 * @since 2024-06-27
 */
@Service
public class PickContainerService extends ServiceImpl<PickContainerMapper, PickContainer> {

    @Autowired
    PickContainerMapper pickContainerMapper;

    /**
     * 移除托盘关系-TODO
     *
     * @param save
     * @return
     */
    @Insert
    @Transactional
    public void removeContainerRelation(PickContainerSave save) throws MessageException {
        if (StringUtils.isNull(save.getPick()) || save.getContainerList().isEmpty()) {
            throw new MessageException(Manager.getMessage(Message.MSG1013.getCode(), "拣配单号和托盘编号"));
        }
        this.update(PickContainer.build(save), new LambdaUpdateWrapper<PickContainer>()
                .eq(PickContainer::getPick, save.getPick())
                .in(PickContainer::getContainer, save.getContainerList())
                .set(PickContainer::getContainerLevel, 1)
                .set(PickContainer::getFatherContainer, null));
    }

    /**
     * 更新站点
     *
     * @param save
     * @return
     */
    @Insert
    @Transactional
    public void updateStation(PickContainerSave save) throws MessageException {
        if (StringUtils.isNull(save.getStation())) {
            throw new MessageException(Manager.getMessage(Message.MSG1013.getCode(), "站点"));
        }
        // 更新站点
        this.update(PickContainer.build(save), new LambdaQueryWrapper<PickContainer>()
                .eq(PickContainer::getId, save.getId()));
    }

    /**
     * 更改拣配单托盘状态
     * @param containers
     * @param status
     */
    public void updateStatusByContainers(List<String> containers, String status) {
        if (!containers.isEmpty()) {
            this.update(PickContainer.build(Integer.parseInt(status)),
                    new LambdaQueryWrapper<PickContainer>().in(PickContainer::getContainer, containers));
        }
    }

    /**
     * 根据优先级获取带配送的托盘
     * @param pickId
     * @return
     */
    public List<PickContainer> trayPriority(String pickId) {
        return pickContainerMapper.trayPriority(pickId);
    }

    /**
     * 查找所有拣配完成的站点信息
     * @param point
     * @return
     */
    public PickContainer occupiedSite(String point) {
        return this.getOne(new LambdaQueryWrapper<PickContainer>().eq(PickContainer::getStation, point)
                .lt(PickContainer :: getStatus, 20));
    }

    /**
     * 根据拣配单号获取托盘信息
     * @param pickId
     * @return
     */
    public List<PickContainer> findByPickId(Integer pickId) {
        return pickContainerMapper.findByPickId(pickId.toString());
    }
}
